package com.sunny.service.v1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Sunny
 * @date 2020/2/6
 */
public class RpcProxyServer {

    ExecutorService executorService = Executors.newCachedThreadPool();

    /**
     * 发布服务
     *
     * @param service 发布的服务
     * @param port 发布的端口
     */
    public void publish(Object service, int port) {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            for (;;){
                //建立连接  BIO
                Socket socket = serverSocket.accept();

                //通过线程来避免IO阻塞
                executorService.execute(new ProcessHandler(socket, service));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
